import { BaseItem } from "../../common/BaseItem";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { Language } from "../../../../../extend/language/Language";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ItemData } from "../../../resource/info/ItemData";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { ImageInfo } from "../../../../../com/gengine/resource/info/ImageInfo";
import { RolePlayer } from "../../../scene/player/entity/RolePlayer";
import { LayerManager } from "../../../manager/LayerManager";
import { TweenLite } from "../../../../../com/greensock/TweenLite";
import { Global } from "../../../../../com/gengine/global/Global";
import { Vector } from "../../../../../__AS3__/vec/Vector";
type int = number;
//class SixtyMaterialCanvas
    
    export  class SixtyMaterialCanvas /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._materials = new Vector.<BaseItem>();
            this.createChildren();
            this.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMouseDown);
            return;
        }

        protected createChildren(): void
        {
            var loc2=null;
            var loc3=null;
            ObjCreate.createBg(0, 0, 142, 120, this, ImagesConst.ToolTipBg);
            var loc1=ObjCreate.createEmbedTitleTextField(Language.getString(60086));
            this.addChild(loc1);
            loc1.x = (142 - loc1.textWidth) / 2;
            loc1.y = 2;
            ObjCreate.createBg(5, 28, 130, 87, this);
            var loc4=0;
            while (loc4 < 6) 
            {
                loc2 = GlobalClass.getBitmap(ImagesConst.PackItemBg);
                loc2.x = 10 + 41 * loc4 % 3;
                loc2.y = 33 + 40 * Math.floor(loc4 / 3);
                this.addChild(loc2);
                loc3 = new BaseItem();
                loc3.setSize(32, 32);
                loc3.x = loc2.x + 3;
                loc3.y = loc2.y + 3;
                loc3.isDragAble = false;
                loc3.isDropAble = false;
                this.addChild(loc3);
                this._materials.push(loc3);
                ++loc4;
            }
            return;
        }

        public setMaterials(arg1: Array<any>): void
        {
            this.checkAddAndPlayEffect(arg1);
            var loc1=0;
            while (loc1 < this._materials.length) 
            {
                if (loc1 < arg1.length) 
                {
                    this._materials[loc1].itemData = arg1[loc1] as ItemData;
                }
                else 
                {
                    this._materials[loc1].itemData = null;
                }
                ++loc1;
            }
            return;
        }

        /* internal  */checkAddAndPlayEffect(arg1: Array<any>): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            var loc4=false;
            loc2 = 0;
            while (loc2 < arg1.length) 
            {
                loc1 = arg1[loc2] as ItemData;
                loc3 = 0;
                while (loc3 < this._materials.length) 
                {
                    if (!this._materials[loc3].itemData || this._materials[loc3].itemData && this._materials[loc3].itemData.itemCode == loc1.itemCode) 
                    {
                        if (!this._materials[loc3].itemData || this._materials[loc3].itemData.itemAmount < loc1.itemAmount) 
                        {
                            LoaderManager.instance.load(loc1.itemInfo.url, this.onImageLoaded, 3, loc3);
                        }
                        break;
                    }
                    ++loc3;
                }
                ++loc2;
            }
            return;
        }

        /* internal  */onImageLoaded(arg1: ImageInfo): void
        {
            var loc5=null;
            var loc1=RolePlayer.instance.stagePointX;
            var loc2=RolePlayer.instance.stagePointY - 55;
            var loc3=arg1.extData as int;
            var loc4=this.localToGlobal(new Object /* flash.geom.Point */(this._materials[loc3].x, this._materials[loc3].y));
            (loc5 = new Object /* flash.display.Bitmap */(arg1.bitmapData)).x = loc1;
            loc5.y = loc2;
            loc5.width = 32;
            loc5.height = 32;
            LayerManager.topLayer.addChild(loc5);
            TweenLite.to(loc5, 1.5, {"x": loc4.x, "y": loc4.y, "onComplete": this.onFlyComplete, "onCompleteParams":[loc5]});
            return;
        }

        /* internal  */onFlyComplete(arg1: Object /* flash.display.Bitmap */): void
        {
            if (arg1 && arg1.parent) 
            {
                arg1.parent.removeChild(arg1);
                arg1 = null;
            }
            return;
        }

        /* internal  */onMouseDown(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (arg1.localY < 28 && arg1.target == this) 
            {
                startDrag();
                Global.stage.addEventListener(flash.events.MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            }
            return;
        }

        /* internal  */onStageMouseUpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            stopDrag();
            Global.stage.removeEventListener(flash.events.MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            return;
        }

        /* internal */ /* var */_materials: Vector.<BaseItem>;
    }
